package com.huaweicloud.servicestage.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

public class CsvUtils {

    public static void writeInstancesCsv(List<String> records) {
        String[] headers = new String[] {"Application Name", "Component Name", "Instance Name", "Application Id",
            "Component Id", "Instance Id"};
        String usrHome = System.getProperty("user.home");
        String exportFilePath = usrHome + File.separator + "ServiceStage_Component.csv";
        File exportFile = new File(exportFilePath);

        try (BufferedWriter bufferedWriter = new BufferedWriter(
            new OutputStreamWriter(new FileOutputStream(exportFile, false), "GB2312"))) {
            bufferedWriter.write(String.join(",", headers));
            bufferedWriter.newLine();
            for (String record : records) {
                bufferedWriter.write(record);
                bufferedWriter.newLine();
            }
            System.out.println("Export result to " + exportFilePath);
        } catch (IOException exception) {
            System.out.println(exception.getMessage());
        }
    }
}
